from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SKU, SPUSpecification
from apps.meiduo_admin.serializers.sku_serializer import SKUSerializer, SKUCategorieSerializer, GoodsSimpleSerializer, \
    GoodsSpecSerialzier
from apps.meiduo_admin.utils import PageNum
from rest_framework.generics import ListAPIView
from apps.goods.models import GoodsCategory
from apps.goods.models import SPU


class SKUModelViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSerializer
    pagination_class = PageNum


class SKUCategoriesView(ListAPIView):

    serializer_class = SKUCategorieSerializer
    # 或者
    queryset = GoodsCategory.objects.filter(subs=None)


class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = SPU.objects.all()


# 规格的视图
class GoodsSpecView(ListAPIView):

    serializer_class = GoodsSpecSerialzier
    # 因为我们继承的是ListAPIView，在拓展类中是通过get_queryset获取数据，
    # 但是我们现在要获取的是规格信息，所以重写get_queryset

    def get_queryset(self):
        pk = self.kwargs['pk']
        return SPUSpecification.objects.filter(spu_id=pk)
